package cn.sxw.ld.module.pet.service.impl;


import cn.sxw.ld.common.exception.CustomException;
import cn.sxw.ld.common.utils.MsgUtils;
import cn.sxw.ld.module.member.pojo.Member;
import cn.sxw.ld.module.member.service.MemberService;
import cn.sxw.ld.module.pet.mapper.PetMapper;
import cn.sxw.ld.module.pet.pojo.Pet;
import cn.sxw.ld.module.pet.service.PetService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: SXW
 * @since: 2025年02月25日 13:47
 * @desc:
 */
@Service

public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements PetService {

    private final MemberService memberService;

    @Autowired
    public PetServiceImpl(@Lazy MemberService memberService) {
        this.memberService = memberService;
    }


    @Value("${ling-dan.userInfo.admin}")
    private String ADMIN;

    @Value("${ling-dan.userInfo.adminPhone}")
    private String ADMIN_PHONE;


    /**
     * @param page
     * @param pageSize
     * @param phone
     * @param petName    宠物名
     * @param memberName 会员名
     * @return
     */
    @Override
    public Page getPage(int page, int pageSize, String phone, String petName, String memberName) {
        LambdaQueryWrapper<Pet> wrapper = new LambdaQueryWrapper<>();
        // 按照名字添加查询条件
        wrapper.like(StringUtils.isNoneBlank(memberName), Pet::getMemberName, memberName);
        wrapper.like(StringUtils.isNoneBlank(petName), Pet::getPetName, petName);
        // 按照手机添加查询条件
        if (StringUtils.isNoneBlank(phone)) {
            wrapper.eq(Pet::getPhone, phone);
        }
        wrapper.eq(Pet::getIsDeleted, 0);
        wrapper.orderByDesc(Pet::getCreateTime);
        Page pageInfo = new Page(page, pageSize);
        this.page(pageInfo, wrapper);
        return pageInfo;
    }

    @Override
    public Map<Integer, String> addPet(Pet pet) {
        HashMap<Integer, String> map = new HashMap<>();
        String phone = pet.getPhone();
        if (StringUtils.isNoneEmpty(phone)) {
            Member byId = memberService.getById(phone);
            if (byId != null) {
                pet.setMemberName(byId.getMemberName());
                int resultCode = this.save(pet) ? 1 : 2;
                MsgUtils.setMsg(resultCode, map, "添加成功", "保存失败，请稍后再试~~");
                return map;
            } else {
                MsgUtils.setMsg(3, map, "添加成功", "保存失败，未查询到手机号对应客户！");
                return map;
            }
        } else {
            pet.setPhone(ADMIN_PHONE);
            pet.setMemberName(ADMIN);
            int resultCode = this.save(pet) ? 1 : 2;
            MsgUtils.setMsg(resultCode, map, "添加成功", "保存失败，请稍后再试~~");
            return map;
        }
    }

    @Override
    public Map<Integer, String> updateCheck(Pet pet) {
        HashMap<Integer, String> map = new HashMap<>();
        String phone = pet.getPhone();
        // 如果修改主人信息为空或者手机号改成了管理员手机号
        if (StringUtils.isEmpty(phone) || StringUtils.compare(phone, ADMIN_PHONE) == 0) {
            // 保存为管理员信息
            pet.setPhone(ADMIN_PHONE);
            pet.setMemberName(ADMIN);
            int resultCode = this.updateById(pet) ? 1 : 2;
            MsgUtils.setMsg(resultCode, map, "修改成功！", "修改失败，请稍后再试~~");
            return map;
        } else {
            Member member = memberService.getById(phone);
            if (member == null) {
                MsgUtils.setMsg(3, map, "修改成功！", "未查询到会员，请检查手机号~~");
                return map;
            }
            pet.setMemberName(member.getMemberName());
            int resultCode = this.updateById(pet) ? 1 : 2;
            MsgUtils.setMsg(resultCode, map, "修改成功！", "修改失败，请稍后再试~~");
            return map;
        }
    }

    @Override
    public boolean batchDeletePets(List<Long> ids) {
        LambdaUpdateWrapper<Pet> petWrapper = new LambdaUpdateWrapper<>();
        petWrapper.in(Pet::getId, ids).set(Pet::getIsDeleted, 1);
        return this.update(petWrapper);
    }

    public boolean batchDeletePetsByMemberIds(List<String> ids) {
        LambdaQueryWrapper<Pet> petWrapper = new LambdaQueryWrapper<>();
        petWrapper.in(Pet::getPhone, ids);
        List<Pet> pets = this.list(petWrapper);
        List<Long> petIds = pets.stream().map(Pet::getId).toList();
        boolean b = this.batchDeletePets(petIds);
        if (!b) {
            throw new CustomException("宠物批量删除失败");
        }
        return true;
    }

}
